#include "slist.h"
#include <stdlib.h>
#include <stdio.h>

int slist_insert(NODE** head,DATA data,DATA ins)
{
	NODE *p = *head;
	NODE *pNEW = (NODE*)malloc(sizeof(NODE));
	NODE *q = NULL;
	if(!pNEW)
	{	
		puts("内存创建失败");
		return -1;
	}
	
	if(!p)
	{
		pNEW -> data = ins;
		pNEW -> next = NULL;
		*head = pNEW;
		return 0;
	}
	
	while(p)
	{
		q = p;
		p = p -> next;
		if(q -> data == data)
		{
			pNEW -> data = ins;
			pNEW -> next = p;
			q -> next = pNEW;
			return 0;
		}
	}
	
	if(!p)
	{
		pNEW -> data = ins;
		pNEW -> next = NULL;
		q -> next = pNEW;
		return 0;
	}
}

void slist_print(NODE *head)
{
	NODE *p = head;
	while(p)
	{
		printf("%d\t",p -> data);
		p = p -> next;
	}
	printf("\n");
}

void slist_free(NODE **head)
{
	NODE *p = *head;
	NODE *q = NULL;
	while(p)
	{
		q = p;
		p = p -> next;
		free(q);
	}
	*head = NULL;
}
